home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5458 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.0 KB  |  58 lines

  1. Newsgroups: comp.lang.c++
  2. Path: news3.noc.netcom.net!zdc!zippo!usenet
  3. From: txwang@public.sta.net.cn (Wang)
  4. Subject: Re: Heap Memory Management
  5. X-Newsreader: Forte Free Agent 1.0.82
  6. Sender: usenet@news.zippo.com
  7. Nntp-Posting-Host: ts1-15.sta.net.cn
  8. Organization: No Organization
  9. Message-ID: <DM9z8L.4Ep@news.zippo.com>
  10. References: <1996Feb4.140550.4699@wisipc.weizmann.ac.il>
  11. Date: Sun, 4 Feb 1996 23:42:45 GMT
  12.  
  13. Kajdan Dimitry <cerlpvk> wrote:
  14.  
  15. | Hi.
  16. | Would someone explain please.
  17.  
  18. | Suppose one got a function that allocates memory on the free store.
  19.  
  20. | char* func(int n)
  21. | {
  22. | char* a=new char[n];
  23. | strcpy(a,"Hello");
  24. | return a;
  25. | }
  26.  
  27. | int main()
  28. | {
  29.  
  30. | char* b=func(10);
  31.  
  32. | return(0);
  33.  
  34. | }
  35.  
  36. | The question is how the memory allocated by new can be freed, since as soon as 
  37. | func() terminates the user loses access to it.
  38. | Thanks a lot
  39.  
  40. No, you haven't lost access to it. You assigned the pointer to b,
  41. right? So,
  42.  
  43. int main()
  44. {
  45. char *b = func(10);
  46. delete b;
  47. return 0;
  48. }
  49.  
  50. will do what you want.
  51.  
  52. Have fun.
  53.  
  54. --
  55. Wang TianXing
  56.  
  57.  
  58.